package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.common.enums.FileTypeEnum;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.IamFileMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.File;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.IamFileService;
import com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.TemptasksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangcs on 2016/8/8.
 */

@Service
public class FileServiceImpl implements IamFileService
{

    @Autowired
    private IamFileMapper iamFileMapper;
    @Autowired
    private TemptasksService temptasksService;

    @Override
    public int insertFile(String businessId, String[] filenames, String[] hbasepaths) {
        int j = 0;
        if( filenames != null && filenames.length > 0 ){
            List<File> list = new ArrayList<File>();
            File file;
            for (int i=0;i<filenames.length;i++){
                file = new File();
                file.setFileId(UniqueUtil.uuid());
                file.setFileName(filenames[i]);
                file.setFileHbasePath(hbasepaths[i]);
                String fileStr = hbasepaths[i];
                fileStr = fileStr.substring(fileStr.lastIndexOf(".")+1);
                file.setFileType(convertFileType(fileStr));
                file.setBusinessId(businessId);
                list.add(file);
            }
            j =  iamFileMapper.insert(list);
        }
        return j;
    }

    @Override
    public int insertFile(String businessId, String files) {
        if(files==null || files.equals("")){
            return -1;
        }
        List<File> listFiles =  JSON.parseArray(files,File.class);
        if(listFiles==null || listFiles.size()==0){
            return -1;
        }
        int len = listFiles.size();
        for(int i=0;i<len;i++){
            File file = listFiles.get(i);
            file.setFileId(UniqueUtil.uuid());
            file.setBusinessId(businessId);
        }
        return iamFileMapper.insert(listFiles);
    }

    @Override
    public int delFileByBusinessId(String businessId) {
        return iamFileMapper.delFileByBusinessId(businessId);
    }

    @Override
    public List<File> queryFilesByBusinessId(String businessId) {
        return iamFileMapper.queryFilesByBusinessId(businessId);
    }

    @Override
    public JSONObject list(BootstrapSearchBean bootstrapSearchBean) {
        JSONObject jsonObject = new JSONObject();
        bootstrapSearchBean.setLimit( bootstrapSearchBean.getLimit() == null ? 10 : bootstrapSearchBean.getLimit() );
        bootstrapSearchBean.setOffset( bootstrapSearchBean.getOffset() == null ? 0 : bootstrapSearchBean.getOffset() );
        //bootstrapSearchBean.setFilterMap(BootstrapUtil.filterStringToMap(bootstrapSearchBean.getFilter()));
        jsonObject.put("total", iamFileMapper.queryCount(bootstrapSearchBean));
        jsonObject.put("rows", JSON.toJSON(iamFileMapper.queryList(bootstrapSearchBean)));
        return jsonObject;
    }

    @Override
    public File selectByPrimaryKey(String fileId) {
     return iamFileMapper.selectByPrimaryKey(fileId);
    }

    @Override
    public List<File> queryListByIds(String[] ids){
        List<File> fileList = iamFileMapper.queryListByIds(ids);
        return fileList;
    }

    @Override
    public int deleteByPrimaryKey(String fileId) {
        return iamFileMapper.deleteByPrimaryKey(fileId);
    }

    @Override
    public int newInsertFile(String uploadFiles, String deletedFiles,String businessId){
        int retCount = 0;
        //更新的附件不为空的情况
        if(uploadFiles!=null && !uploadFiles.equals("")){
            //强制转换成集合
            List<File> listFiles =  JSON.parseArray(uploadFiles,File.class);
            if(listFiles!=null && listFiles.size()>0){
                int len = listFiles.size();
                for(int i=0;i<len;i++){
                    File _file = listFiles.get(i);
                    //设置主键
                    _file.setFileId(UniqueUtil.uuid());
                    //设置业务数据id
                    _file.setBusinessId(businessId);
                    _file.setFileType(FileTypeEnum.getCode(_file.getExt()));
                }
                retCount += iamFileMapper.insert(listFiles);
            }
        }
        //删除附件不为空的情况
        if(deletedFiles != null && !deletedFiles.equals("")){
            //强制转换成集合
            List<File> listFiles =  JSON.parseArray(deletedFiles,File.class);
            if(listFiles!=null && listFiles.size()>0){
                int len = listFiles.size();
                for(int i=0;i<len;i++){
                    File _file = listFiles.get(i);
                    //删除hbase中的数据
                    //retCount += temptasksService.delfile(_file.getFileHbasePath());
                    //删除附件表中的数据
                    retCount += iamFileMapper.deleteByPrimaryKey(_file.getFileId());
                    }
                }
            }
        return retCount;
    }


    /**
     * 对文件类型做处理
     * @param fileType
     * @return
     */
    public int convertFileType(String fileType){
        if(fileType==null || "".equals(fileType)) return -1;
        if(fileType.equals("txt")) return 1;
        if(fileType.equals("doc")) return 2;
        if(fileType.equals("docx")) return 3;
        if(fileType.equals("xls")) return 4;
        if(fileType.equals("xlsx")) return 5;
        if(fileType.equals("ppt")) return 6;
        if(fileType.equals("pptx")) return 7;
        if(fileType.equals("rar")) return 8;
        if(fileType.equals("pdf")) return 9;
        if(fileType.equals("zip")) return 10;

        //视频格式
        if(fileType.equals("rm")) return 11;
        if(fileType.equals("rmvb")) return 12;
        if(fileType.equals("wmv")) return 13;
        if(fileType.equals("avi")) return 14;
        if(fileType.equals("mp4")) return 15;
        if(fileType.equals("mkv")) return 16;
        if(fileType.equals("flv")) return 17;
        if(fileType.equals("f4v")) return 18;
        return -1;
    }
}
